package com.bifrii.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import com.bifrii.transferObjects.TopEvent;

public class JDBCTopEventDAO implements ITopEventDAO {

	@Override
	public List<TopEvent> selectTopEvents(Integer top) {
		Connection conn = ConnectionManager.getInstance().checkOut();

        PreparedStatement stmt = null;
        List<TopEvent> searchResults = new LinkedList<TopEvent>();
        ResultSet result = null;

        try {

            String sql = "SELECT * FROM TopEvent order by users desc, votes desc limit 0,?";
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, top);
            stmt.executeQuery();
            result = stmt.executeQuery();

            while (result.next()) {
                TopEvent temp = new TopEvent();
                temp.setOid(result.getString("oid"));
                temp.setIdEvent(result.getInt("idEvent"));
                temp.setUsers(result.getInt("users"));
                temp.setVotes(result.getInt("votes"));
                
                searchResults.add(temp);
            }
            
        } catch (SQLException ex) {
            System.out.println("Message: " + ex.getMessage());
            System.out.println("SQLState: " + ex.getSQLState());
            System.out.println("ErrorCode: " + ex.getErrorCode());
        } finally {
            ConnectionManager.getInstance().checkIn(conn);
            try {
                if (result != null)
                    result.close();
                if (stmt != null)
                    stmt.close();
            } catch (SQLException ex) {
            }

        }
        return searchResults;
	}

	@Override
	public void insert(TopEvent e) {
		Connection conn = ConnectionManager.getInstance().checkOut();
		String sql = "INSERT INTO TopEvent ( oid, idEvent, users, votes) VALUES (?, ?, ?, ?) ";
        PreparedStatement stmt = null;

        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, e.getOid());
            stmt.setInt(2, e.getIdEvent());
            stmt.setInt(3, e.getUsers());
            stmt.setInt(4, e.getVotes());


            stmt.executeUpdate();

        } catch (SQLException ex) {
            System.out.println("Message: " + ex.getMessage());
            System.out.println("SQLState: " + ex.getSQLState());
            System.out.println("ErrorCode: " + ex.getErrorCode());
        } finally {
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException ex) {
            }
        }
	}

	@Override
	public void delete(Integer idEvent) {
		Connection conn = ConnectionManager.getInstance().checkOut();
		
		String sql = "DELETE FROM TopEvent WHERE (idEvent = ?) ";
        PreparedStatement stmt = null;

        try {
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, idEvent);
            stmt.executeUpdate();
        } catch (SQLException e) {
            System.err.println("Message: " + e.getMessage());
            System.err.println("SQLState: " + e.getSQLState());
            System.err.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }
        }
	}

}
